﻿using Rsd.Dudu;
using Rsd.Dudu.Core;
using Rsd.Dudu.Helpers;
using Rsd.Faith.Core;
using Rsd.Faith.Fiscal.Core;
using Rsd.Faith.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace Rsd.Faith.Fiscal.Services
{
    /// <summary>
    /// 
    /// </summary>
    public class BookService : BaseService, IBookService
    {
        public override ServiceProxcy Proxcy =>  ServiceProxcy.Sington;
        
        /// <summary>
        /// 建账
        /// </summary>
        /// <param name="xiaoQuBianHao"></param>
        /// <param name="xiaoQuMingCheng"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public BookXiaoQuZongZhangModel NewXiaoQuAccount(string xiaoQuBianHao, string xiaoQuMingCheng, DateTime date, bool autoSave = true)
        {
            var account = MyType.GetObject<BookXiaoQuZongZhangModel>(obj => obj.XiaoQuBianHao == xiaoQuBianHao);
            if (account == null)
            {

                var _no = StringHelper.SqlFilter("C" + xiaoQuBianHao);

                account = MyType.GetObject<BookXiaoQuZongZhangModel>(obj => obj.ZhangHuBianHao == _no);
                if (account == null)
                {
                    account = new BookXiaoQuZongZhangModel();
                    account.ZhangHuBianHao = _no;
                    account.JianZhangRiQi = date;
                    account.XiaoQuMingCheng = xiaoQuMingCheng;
                    account.XiaoQuBianHao = xiaoQuBianHao;
                    account.ZhuangTai = ZhuangHuZhuangTai.DG;
                    account.SetState(Dudu.BizModel.ObjectStateType.New);
                    if (autoSave)
                        MyType.GetType<BookXiaoQuZongZhangModel>().SaveObject(account);
                }


            }
            return account;
        }

        /// <summary>
        /// 建账
        /// </summary>
        /// <param name="fangWuBianHao"></param>
        /// <returns></returns>
        public BookFenHuZhangModel NewFenHuAccount(string fangWuBianHao, string xiaoQuBianHao, DateTime date, bool autoSave = true)
        {
            var account = MyType.GetObject<BookFenHuZhangModel>(obj => obj.FangWuBianHao == fangWuBianHao);
            if (account == null)
            {

                var _no = StringHelper.SqlFilter("F" + fangWuBianHao);

                account = MyType.GetObject<BookFenHuZhangModel>(obj => obj.ZhangHuBianHao == _no);
                if (account == null)
                {
                    account = new BookFenHuZhangModel();
                    account.ZhangHuBianHao = _no;
                    account.JianZhangRiQi = date;
                    account.FangWuBianHao = fangWuBianHao;
                    account.XiaoQuBianHao = xiaoQuBianHao;
                    account.ZhuangTai = ZhuangHuZhuangTai.DG;
                    account.SetState(Dudu.BizModel.ObjectStateType.New);
                    if (autoSave)
                        MyType.GetType<BookFenHuZhangModel>().SaveObject(account);

                }


            }
            return account;
        }

        /// <summary>
        /// 建账
        /// </summary>
        /// <param name="danYuanHao"></param>
        /// <param name="louDongHao"></param>
        /// <param name="xiaoQuBianHao"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public BookDanYuanMingXiZhangModel NewDanYuanAccount(string danYuanHao, string louDongHao, string xiaoQuBianHao, DateTime date, bool autoSave = true)
        {
            var account = MyType.GetObject<BookDanYuanMingXiZhangModel>(obj => obj.XiaoQuBianHao == xiaoQuBianHao && obj.LouDongHao == louDongHao && obj.DanYuanHao == danYuanHao);
            if (account == null)
            {
                var _no = StringHelper.SqlFilter("C" + xiaoQuBianHao + "L" + louDongHao.PadLeft(3, '0') + "D" + this.GetDanYuanHaoNumber(danYuanHao));

                account = MyType.GetObject<BookDanYuanMingXiZhangModel>(obj => obj.ZhangHuBianHao == _no);
                if (account == null)
                {
                    account = new BookDanYuanMingXiZhangModel();
                    account.ZhangHuBianHao = _no;
                    account.JianZhangRiQi = date;
                    account.DanYuanHao = danYuanHao;
                    account.LouDongHao = louDongHao;
                    account.XiaoQuBianHao = xiaoQuBianHao;
                    account.ZhuangTai = ZhuangHuZhuangTai.DG;

                    account.SetState(Dudu.BizModel.ObjectStateType.New);
                    if (autoSave)
                        MyType.GetType<BookDanYuanMingXiZhangModel>().SaveObject(account);
                }


            }
            return account;
        }

        /// <summary>
        /// 建账
        /// </summary>
        /// <param name="louDongHao"></param>
        /// <param name="xiaoQuBianHao"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public BookLouDongMingXiZhangModel NewLouDongAccount(string louDongHao, string xiaoQuBianHao, DateTime date, bool autoSave = true)
        {
            var account = MyType.GetObject<BookLouDongMingXiZhangModel>(obj => obj.XiaoQuBianHao == xiaoQuBianHao && obj.LouDongHao == louDongHao);
            if (account == null)
            {

                var _no = StringHelper.SqlFilter("C" + xiaoQuBianHao + "L" + louDongHao.PadLeft(3, '0'));

                account = MyType.GetObject<BookLouDongMingXiZhangModel>(obj => obj.ZhangHuBianHao == _no);
                if (account == null)
                {
                    account = new BookLouDongMingXiZhangModel();
                    account.ZhangHuBianHao = _no;
                    account.JianZhangRiQi = date;
                    account.LouDongHao = louDongHao;
                    account.XiaoQuBianHao = xiaoQuBianHao;
                    account.ZhuangTai = ZhuangHuZhuangTai.DG;
                    account.SetState(Dudu.BizModel.ObjectStateType.New);

                    if (autoSave)
                        MyType.GetType<BookLouDongMingXiZhangModel>().SaveObject(account);
                }


            }
            return account;
        }

        /// <summary>
        ///  暂无使用 核算分户账
        /// 分户 重新计算（缴存、使用、利息收入、更新所属期、记当期期前余额）
        /// </summary>
        /// <param name="xiaoQuBianHao">小区编号</param>
        /// <param name="suoShuQi">更新所属期</param>
        /// <returns></returns>
        public int FenHuAccounting(string xiaoQuBianHao, string suoShuQi)
        {
            throw new NotImplementedException("BookAccounting");

            var sqls = new List<string>();

            //核算分户账目 (缴存) book__fen_hu_zhang
            //var sql_update_jiaocun = string.Format(@"update book__fen_hu_zhang A,( select sum(shi_jiao_jin_e) as 'jiao_cun_jin_e_zz' from fiscal__piao_ju_xin_xi_biao where fang_wu_dai_ma = '{0}' and zhuang_tai='{1}' ) B set A.jiao_cun_jin_e = B.jiao_cun_jin_e_zz where A.zhang_hu_bian_hao = '{2}';", _fangWuDaiMa, FaPiaoZhuangTai.RZ.ToString(), zhang.ZhangHuBianHao);

            //核算 分户帐(使用) book__fen_hu_zhang
            //var sql_update_fentan = string.Format(@"update book__fen_hu_zhang A,( select sum(fen_tan_jin_e) as 'shi_yong_jin_e' from biz__zi_jin_shi_yong_fen_tan_biao where  zhuang_tai = '{0}' and fang_wu_bian_hao ='{1}' ) B set A.shi_yong_jin_e = B.shi_yong_jin_e where A.zhang_hu_bian_hao = '{2}';", FenTanZhuangTai.RZ.ToString(), _fangWuDaiMa, zhang.ZhangHuBianHao);

            //核算 分户 增值(本期及往属期的 利息收益)


            //更新当前所属期 suoShuQi

            //sqls.AddRange(new string[] { sql_update_jiaocun, sql_update_fentan });

            var dbSession = (IDBSession)this.GetService(typeof(IDBSession), null);

            return dbSession.DbService.Execute(sqls.ToArray());
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        private string GetDanYuanHaoNumber(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                return "00";
            }
            var _rs = name;
            switch (name)
            {
                case "一":
                    _rs = "01";
                    break;
                case "二":
                    _rs = "02";
                    break;
                case "三":
                    _rs = "03";
                    break;
                case "四":
                    _rs = "04";
                    break;
                case "五":
                    _rs = "05";
                    break;
                case "六":
                    _rs = "06";
                    break;
                case "七":
                    _rs = "07";
                    break;
                case "八":
                    _rs = "08";
                    break;
                case "九":
                    _rs = "09";
                    break;
                case "十":
                    _rs = "10";
                    break;
                case "其他":
                    _rs = "00";
                    break;

            }

            return _rs;
        }

        
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override dynamic GetMonitorInfo()
        {
            throw new NotImplementedException();
        }
    }
}
